#########################################################################
#									#
# Name:	 	flag_check.pm (Perl Module)				#
#									#
# Completed:	8th October, 2003.					#
#									#
# Updated:	26th November, 2003.					#
#									#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
#									#
# Arguments:	N/A							#
#									#
# Purpose:	This modules is a LEMS module that is used to check	#
#		for flag files and stop monitors when one exists,	#
#		and to restart them when the flag file is removed.	#
#									#
# Limitations:								#
#									#
# Environment:	None.							#
#									#
# Exit Codes:	N/A							#
# 									#
# Version:	@(#)1.0.0 Original (SE)>				#
# 		@(#)1.0.1 Implement status function (SE)>		#
#									#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#									#
# Complete SOCM Version History						#
#									#
# Version:	1.0.0							#
# Date:		7th October, 2003.					#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
# Change:	Original.						#
#									#
# Version:	1.0.1							#
# Date:		26th November, 2003.					#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
# Change:	Implemented a stat function allowing access to 		#
#		status information from this monitor.			#
#									#
#########################################################################

package flag_check;

sub new {
my $self={};
my $package=$_[1];
my ($r,$directory);

	$directory="/etc/cluster/$package/flags";
	if(! -d $directory) {
		::logwarn("Flags directory $directory does not exist - creating!");
		$r=mkdir($directory);
		if(!$r) {
			::logwarn("Unable to create directory - no flag monitoring!");
			return undef;
		}
	}
	$self->{DIRECTORY}=$directory;
	$self->{PACKAGE}=$package;

	#################################################################
	# We keep the details of the current flags in a reference to	#
	# a hash - obviously empty until the first check.		#
	#################################################################

	$self->{FLAGS}={};
	bless($self);
	return $self;
}

sub check {
my $self=shift;
my ($str,%mapper,$dir,$fd,@new,@startup,@files);

	@files=();
	$dir=$self->{DIRECTORY};
	if(! -d $dir) {
		::logwarn("No such directory $dir!");
		return 0;
	}
	if(!(opendir($fd,$dir))) {
		::logwarn("Unable to open directory $dir!");
		return 0;
	}
	@files=grep {! /^\./ } readdir($fd);
	%mapper=();
	foreach (@files) {
		$mapper{$_}=1;
	}
	closedir($fd);

	#################################################################
	# Create a list of flags that have we do not yet know about.	#
	#################################################################

	foreach (@files) {
		next if exists($self->{FLAGS}->{$_});
		push @new,$_;
	}

	#################################################################
	# Also attempt to find flag files that we know about, but no	#
	# longer exist.							#
	#################################################################

	foreach (keys(%{$self->{FLAGS}})) {
		if(! exists($mapper{$_})) {
			push @startup,$_;
		}
	}

	#################################################################
	# Generate the string to return.				#
	#################################################################

	$str="";
	foreach (@startup) {
		$str.="START $_;";
		delete $self->{FLAGS}->{$_};
	}
	foreach (@new) {
		$str.="STOP $_;";
		$self->{FLAGS}->{$_}=1;
	}
	$::RCDATA=$str;
	return 0 if $str eq "";
	return 1;
}

sub stat {
my $self=shift;

	return join(",",(keys(%{$self->{FLAGS}})));
}
1;

